package com.yidu.ladder.system.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.ladder.system.domain.Customer;
import com.yidu.ladder.system.domain.CustomerVo;
import com.yidu.ladder.system.service.CustomerService;

/**
 * 客户管理系统控制器
 * @author Xiaofei
 *
 */
@Controller
@RequestMapping("/system/")
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	
	/**
	 * 得到客户管理Service
	 * @return 客户管理Service
	 */
	public CustomerService getCustomerService() {
		return customerService;
	}
	
	/**
	 * 修改客户Service
	 * @param customerService
	 */
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	/**
	 * 分页查询客户
	 * @param paramCustomerVo 客户包装类
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "customers", method = RequestMethod.GET)
	public Map<String, Object> queryCustomerByCriteriaWithPage(CustomerVo paramCustomerVo) {
		// 调用分页查询的方法
		Map<String, Object> map = customerService.queryCustomerByCriteriaWithPage(paramCustomerVo);
		return map;
	}

	/**
	 * 添加客户
	 * @param customer 客户
	 * @return 添加成功返回true ，否则返回false
	 */
	@ResponseBody
	@RequestMapping(value = "customer", method = RequestMethod.POST)
	public boolean addCustomer(Customer customer) {
		//调用service层添加客户
		boolean result = customerService.addCustomer(customer);
		return result;
	}

	/**
	 * 修改客户
	 * @param customer 客户
	 * @return 修改成功返回true，否则返回false
	 */
	@ResponseBody
	@RequestMapping(value = "customer", method = RequestMethod.PUT)
	public boolean updateCustomer(Customer customer) {
		System.out.println(customer);
		//调用service层修改客户
		boolean result = customerService.updateCustomer(customer);
		System.out.println(result);
		return result;
	}
	
	/**
	 * 冻结或还原客户
	 * @param customerIds 客户Id
	 * @param customerStatus 是否可用
	 * @return 结果字符串
	 */
	@ResponseBody
	@RequestMapping(value = "customer{customerId}/{customerStatus}", method = RequestMethod.DELETE)
	public String freezeOrRecoverCustomer(@PathVariable("customerId") String customerIds, @PathVariable("customerStatus") Integer customerStatus) {
		System.out.println(customerIds + customerStatus);
		//调用service冻结或还原方法
		boolean result = customerService.freezeOrRecoverCustomer(customerIds, customerStatus);
		//如果结果为false，就返回fail字符串
		if (result == false) {
			return "fail";
		}
		//否则返回success
		return "success";
	}

}
